

package jetbrains.buildServer.server.rest.data.util;

import java.util.Comparator;
import java.util.TreeSet;
import org.jetbrains.annotations.NotNull;

/**
 * Uses given comparator for duplicates checking, effectively internally mainiting sorted order of the checked items.
 */
public class ComparatorDuplicateChecker<ITEM> implements DuplicateChecker<ITEM> {
  private final TreeSet<ITEM> mySeenItems;

  public ComparatorDuplicateChecker(@NotNull Comparator<ITEM> comparator) {
    mySeenItems = new TreeSet<>(comparator);
  }

  @Override
  public boolean checkDuplicateAndRemember(@NotNull ITEM item) {
    return !mySeenItems.add(item);
  }
}